\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots,pgfplotstable,tikz}
\usetikzlibrary{intersections,matrix}
\usepackage{verbatim}
\usepackage{moresize}
\usetikzlibrary{calc}
\usepackage{datatool}
\usetikzlibrary{shapes}
\usetikzlibrary{plotmarks}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{mathtools}
\usepackage{amsmath,amssymb}
\usetikzlibrary{shapes.geometric}
\usepackage{booktabs}
\usepackage{xspace} 
\usepackage{etoolbox}
\usepackage{multirow}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.pathmorphing}
\pgfplotsset{compat=1.13}
\usepackage{fp}
\usepgfplotslibrary{fillbetween}

\definecolor{green}{RGB}{68,126,35}
\definecolor{blue}{RGB}{19,30,92}
\definecolor{red}{RGB}{219,51,24}


% Defines "sgn" function
\def\sgn{\mathop{\rm sgn}\nolimits} 
% Defines the expectation function
\def\E{\mathop{\mathrm{E}}\nolimits} 

% Defines a function that ignores the zero on the y- or the x-axis, such that the origin
% is only labeled once. Activate in the axis options by adding the key: "ignore zero=x" or ""ignore zero=y."
\pgfplotsset{ignore zero/.style={%
		#1ticklabel={\ifdim\tick pt=0pt \else\pgfmathprintnumber{\tick}\fi}
	}}
	
	
	
	\begin{document}	
		
	% TAKE INPUTS FROM THE FOLLOWING FILE:
		% "extrapolation_late.xls" --- generated in Stata by the program file: "extrapolation_late.do" 	(for plotting “MA LATE” on the MTE(p) line and the S.E. reported for "MA LATE" in the figure legend)
		% "extrapolation_slate.xls" --- generated in Stata by the program file: "extrapolation_slate.do" (for plotting the E[MTE(p,X_MA)] line and the S.E. reported for "MA LATE with X_MA" in the figure legend)
		
			\pgfmathsetmacro\pBdi{0.15}
			\pgfmathsetmacro\pIdi{0.41}
			% midpoint for OR compliers
			\pgfmathsetmacro\xcomdi{\pBdi*0.5+\pIdi*0.5}
			\pgfmathsetmacro\yaxismin{-1.1}
			\pgfmathsetmacro\yaxismax{1.15}
			
			\pgfmathsetmacro\ORlate{0.267122474201187} % variable "LATE_ohie" in "extrapolation_late.xls" 
			\pgfmathsetmacro\MAlate{-0.579220862217166} % variable "LATE_brfss" in "extrapolation_late.xls" 
			\pgfmathsetmacro\MTEslope{-1.32189329694664} %slope of the MTE(p) line -- same as other figures
			\pgfmathsetmacro\MTEintercept{0.63892031450335} %intercept of the MTE(p) line -- same as other figures
			
			
			\pgfmathsetmacro\MAlateXMA{-0.787026070547465} % variable "SLATE_ma_xs_ma_ps" in "extrapolation_slate.xls" 
			\pgfmathsetmacro\MTEslopeXMA{-1.88146181075521} %slope of theE[MTE(p,X_MA)] line -- variable "SMTE_term_2_ma_xs_ma_ps" in "extrapolation_late.xls"
			
			% midpoint for MA compliers
			\pgfmathsetmacro\pBMA{0.90}
			\pgfmathsetmacro\pIMA{0.95}
			\pgfmathsetmacro\xcomMA{\pBMA*0.5+\pIMA*0.5}
			
			\pgfmathsetmacro\MTEinterceptXMA{\MAlateXMA-\xcomMA*\MTEslopeXMA} % calculating the slope of the E[MTE(p,X_MA)] line
			

			% MTE(pC) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			\begin{tikzpicture}[>=stealth]
			% Axis options
			\begin{axis}[					
			width=15cm,					% width of chart
			height=10cm,				% height of chart
			xlabel={$U_D$:  unobserved net cost of treatment, Oregon},				% x-axis title
			ylabel={Number of ER Visits},					% y-axis title
			ylabel style={rotate=0},  % rotate the y-axis title
			samples=100,  				% sets the resolution for curved lines
			xmin=0,xmax=1,			    % x-axis range
			ymin=-1.1,
			set layers=standard,
			%ymin=1.2*\Dinterceptr,ymax=1.05*\yaxismax,			% y-axis range
			ignore zero=y,			    % ignore the zero on the y-axis (defined in preamble)
			xtick={0,\pBdi,\pIdi,1},	    % only show these x ticks
			xticklabels={0,$p_C^{OR}=\pBdi$, $p_I^{OR}=\pIdi$,1}, % label x ticks
			ytick={0,\MTEinterceptXMA,\MTEintercept,\ORlate, \MAlate,\MAlateXMA,\MTEinterceptXMA+\MTEslopeXMA,  \MTEintercept+\MTEslope},	    % only show these y ticks
			yticklabels={0 ,0.95,0.64,0.27,-0.58,-0.79, , },	    % only show these y ticks
			ymax = 1.15,
			legend style={draw=none,at={(0.5,\yaxismax-0.18)},anchor=north},	% set legend style, specifically no outline
			legend cell align={left},	% align legend content to the left
			clip=false,					% place nodes outside of plot area
			xticklabel style={
						align=center,
						yshift=-5.5ex,
						name=label,
						append after command={(label.north) edge [solid] ++(0,6ex)},
										/pgf/number format/fixed,
										/pgf/number format/precision=2},
					xlabel style={at={(axis description cs:0.5,-0.17)},anchor=north}, % ncrease distance between title and x-axis
					yticklabel style={	/pgf/number format/fixed,
						/pgf/number format/precision=2},
					% place some extra y ticks ...
					extra y ticks={0},
					% ... but do not repeat the already existent labels
					% (from the "normal" ticks) ...
					extra y tick labels={},
					% ... and set the style of the extra ticks to `major'
					extra tick style={
						grid=major,
					},
		%	ticklabel style={fill=white},
			%xlabel style={align=center,xshift=0pt,yshift=-20pt},
			 mydot/.style={
			 	circle,
			 	fill=white,
			 	draw,
			 	outer sep=0pt,
			 	inner sep=1.5pt
			 },
			]		
			
			\draw [thin,draw=gray] (\pIMA,\yaxismin) -- (\pIMA,\yaxismin+0.04);
			\draw [thin,draw=gray] (\pBMA,\yaxismin) -- (\pBMA,\yaxismin+0.04);
			\draw [thin,draw=gray] (\pIMA,\yaxismax) -- (\pIMA,\yaxismax-0.04);
			\draw [thin,draw=gray] (\pBMA,\yaxismax) -- (\pBMA,\yaxismax-0.04);
			
			
			% mte(p) - Dashed Lines OR
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(\pBdi,\yaxismin) (\pBdi,\MTEintercept+\MTEslope*\pBdi)};
			
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(\pIdi,\yaxismin) (\pIdi,\MTEintercept+\MTEslope*\pIdi)};
			
			% mte(p) - Dashed Lines MA
			
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(\pBMA,\yaxismax) (\pBMA,\MTEinterceptXMA+\MTEslopeXMA*\pBMA)};
		
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(\pIMA,\yaxismax) (\pIMA,\MTEinterceptXMA+\MTEslopeXMA*\pIMA)};
			
			
						
			% mte(p) - MTE Lines and Shading
			\addplot[
				line width=1.8pt, line cap=round, red, name path=OR
			] coordinates {(0,\MTEintercept) (1,\MTEintercept+\MTEslope)}
				%node[pos=0.55,black,pin={[pin edge={black}]90:{\textcolor{black}{\small$\text{MTE}\left(p\right)$}}}]{}
				node[pos=\xcomdi,black,pin={[pin edge={black},pin distance=0pt]-90:{\textcolor{black}{\small }}}]{};
				\label{plot:mte} 
				
				
			\node[label={[align=center]0:{OR \\ LATE}},circle,fill,inner sep=2pt,opacity=0] at (axis cs:0.23,0.12) {};				
				
			
			\addplot[domain=0:1, line width=1.2pt, black!15!red, opacity=0.66, name path=MA]
				{\MTEinterceptXMA+\MTEslopeXMA*\x};
				\label{plot:mtex}
				
			% add legend
			% \legend{,,,,{\small $\text{MTE}(p)$},\small {$\E[\text{MTE}(X_{\text{MA}},p)]$}: {age, female, English}}
				
			% Add right-hand yticklabel
			\node[anchor=west] () at (axis cs: 1,\MTEinterceptXMA+\MTEslopeXMA) {$-0.93$};
			\node[anchor=west] () at (axis cs: 1,\MTEintercept+\MTEslope) {$-0.68$};
			

			
			\node[label={[align=center]0:{MA \\ LATE}},circle,fill,inner sep=2pt,opacity=0] at (axis cs:0.78,-0.3) {};
			\draw [thick,black] (\xcomMA,\MTEintercept+\xcomMA*\MTEslope) -- (0.87,-0.4);
			
						\node[label={[align=center]0:{MA LATE \\ with $X_{MA}$}},circle,fill,inner sep=2pt,opacity=0] at (axis cs:0.735,-0.86) {};	
			\draw [thick,black] (\xcomMA,\MTEinterceptXMA+\xcomMA*\MTEslopeXMA) -- (0.88,-0.86);

			% mte(p) - LATE Labels
			\node at (axis cs:\xcomdi,\MTEintercept/2) {};
			
			
						% Add points
			\node[outer sep=0pt,diamond, fill=red,inner sep=1.75pt] (P) at (\xcomdi, \ORlate) {};

			\node[outer sep=0pt,diamond, fill=black!20!white!20!red,inner sep=1.75pt] (P) at (\xcomMA, \MAlateXMA) {};
			\node[outer sep=0pt,diamond, fill=red,inner sep=1.75pt] (P) at (\xcomMA, \MAlate) {};
			
			
		
						\node[label={[align=center, fill=none]-270:{\small $p_{I}^{MA}$\\ $\pIMA$}},circle,fill=none,inner sep=2pt,opacity=0] at (axis cs:\pIMA+0.005,\yaxismax+0.2) {};

						\draw [thin,black] (\pIMA,\yaxismax) -- (\pIMA,\yaxismax+0.2);
						
						\node[label={[align=center, fill=none]-270:{\small $p_{C}^{MA}$  \\ $\pBMA$}},circle,fill=none,inner sep=2pt,opacity=0] at (axis cs:\pBMA-0.005,\yaxismax+0.2) {};
						\draw [thin,black] (\pBMA,\yaxismax) -- (\pBMA,\yaxismax+0.2);

						\node[label={[align=left,fill=white]-270:{0}},circle,fill,inner sep=2pt,opacity=0] at (axis cs:0,\yaxismax+0.2) {};
						\draw [thin,black] (0,\yaxismax) -- (0,\yaxismax+0.2);
						
						\node[label={[align=left,fill=white]-270:{1}},circle,fill,inner sep=2pt,opacity=0] at (axis cs:1,\yaxismax+0.2) {};
						\draw [thin,black] (1,\yaxismax) -- (1,\yaxismax+0.2);
						
								
%Put two Complier group labels on x-axis
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pBdi,-1.1) -- (\pIdi, -1.1) node[pos=0.5, below, yshift = 0pt] {Compliers};		
			
\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (0,\yaxismin) -- (\pBdi, \yaxismin) node[pos=0.5, below, yshift = 0pt, align=center] {Always \\ Takers};
			
\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pIdi,\yaxismin) -- (1,\yaxismin) node[pos=0.5, below, yshift = 0pt] {Never Takers};				
	
\draw[draw=none,decorate,decoration={brace,amplitude=10pt,raise=4pt},yshift=0pt] (0,\yaxismax) -- (\pBMA, \yaxismax) node[pos=0.5, above, yshift = 0pt] {Always Takers};	
			
\draw[draw=none,decorate,decoration={brace,amplitude=10pt,raise=4pt},yshift=0pt] (\pBMA,\yaxismax) -- (\pIMA, \yaxismax) node[pos=0.5, above, yshift = 0pt] {C};		

\draw[draw=none,decorate,decoration={brace,amplitude=10pt,raise=4pt},yshift=0pt] (\pIMA,\yaxismax) -- (1, \yaxismax) node[pos=0.5, above, yshift = 0pt] {NT};		

			
			\node[label={[align=center,fill=none]-270:{$U_D$: unobserved net cost of treatment, Massachusetts}},circle,fill,inner sep=2pt,opacity=0] at (axis cs:0.5,\yaxismax+0.3) {};
		
		
		%\draw[draw=none,decorate,decoration={brace,amplitude=10pt,raise=4pt},yshift=0pt] (0,\yaxismax) -- (\pBMA,\yaxismax) node[pos=0.5, above, yshift = 0pt] {MA Always Takers};

		%\draw[decorate,decoration={brace,amplitude=4pt,raise=2pt},yshift=0pt] (\pIMA,\yaxismax) -- (1, \yaxismax);
		
		%\node[label={[align=center]0:{MA Never \\ Takers}},circle,fill,inner sep=2pt,opacity=0] at (axis cs:1.01,\yaxismax+0.19) {};
		%\draw [thin,black] (0.97,\yaxismax+0.06) -- (1.035,\yaxismax+0.19);
				
		
			
			% Legend
						\coordinate (legend) at (axis description cs:1,1);
		
		


			\end{axis}
		
			\matrix [
				draw,
				fill=white,
				,nodes={element},matrix of nodes,
				anchor=north east,
				inner sep=0pt,outer sep=0pt, every node/.style={inner ysep=2pt,inner xsep=1pt},
			] at (legend) {
										   	  & \footnotesize \hspace{-5pt}MA LATE   & \footnotesize		S.E.$\quad$			 	   \\ \midrule
		\footnotesize $$ \ref{plot:mte} $ \text{MTE}(p)\hspace{0.55in}$ &\footnotesize $-0.58^{}$ &\footnotesize $(0.59)\, \quad$   \\
		\footnotesize $$\ref{plot:mtex} $ E[\text{MTE}(p, X_{MA})]$ &\footnotesize $-0.79^{}$ &\footnotesize $(0.58)\, \quad$  \\ \\
					        };		
		
			\end{tikzpicture}
	
		
	\end{document}